<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Leave;
use Illuminate\Support\Facades\Log;

class LeaveService
{
    public function get(){
        $sql = Leave::with('application')->with('user');
        return $sql;
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        $res['role_id'] = 0;
        try {
            $data = Leave::create($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            $v = $v === null ? '/' : $v;

            if($v){
                $res[$k] = $v;
            }
        }
        $res['role_id'] = 0;
        unset($res['id']);
        try {
            $data = Leave::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }
}